﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebUI.Pages
{
    public partial class mgrFabricantes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get credentials
            UserController usrController = new UserController();
            int secUsuario = usrController.GetUserID(User.Identity.Name);
            int secTeam = usrController.GetUserTeamID(secUsuario);

            if (secTeam != 9)
            {
                Response.Redirect("../Pages/403.aspx");
            } 
            
            if (!this.IsPostBack)
            {
                FabricanteController fabController = new FabricanteController();
                rpFabricantes.DataSource = fabController.GetFabricantes();
                rpFabricantes.DataBind();
            }
        }

        protected void cmdNovo_Click(object sender, EventArgs e)
        {
            try
            {
                FabricanteController fabController = new FabricanteController();
                fabController.NewFabricante(txtNome.Text);
            }
            catch (Exception ex)
            {
                lblMsg.Text = "Erro ao cadastrar fabricante. " + ex.Data;
            }
            finally
            {
                txtNome.Text = "";
                Response.Redirect("../Pages/mgrFabricantes.aspx");
            }
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            FabricanteController fabController = new FabricanteController();
            rpFabricantes.DataSource = fabController.GetFabricantes();
            rpFabricantes.DataBind();
        }

        protected void rpFabricantes_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "cmdEdit")
            {
                Response.Redirect("../Pages/editFabricante.aspx?id=" + e.CommandArgument.ToString());
            }
        }
    }
}